package controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import model.ByGroupModel;
import model.ByNameModel;
import model.Contact;
import model.ContactListModel;
import view.ContactWindow;
import view.MainWindow;

/**
 * Redirect to the model which order the contact by group
 * @author Marie Morin
 *
 */
public class ByGroupController implements ActionListener{
	
	private MainWindow w;
	private ContactWindow a;
	private String text;	
	private ByGroupModel m;
	private ArrayList<Contact> contactsList = new ArrayList<Contact>();
	
	/**
	 * Redirect to the model to select only the contacts that are in the group entered by the user in the field
	 * @param w, the main JFrame of the application
	 * @param a, the contact window needed to get the text typed by the user on the search field
	 */
	public ByGroupController(MainWindow w, ContactWindow a){
		
		this.w = w;
		this.a=a;
		this.text = "";	

		// Create a list with all the contacts of the file 
		ContactListModel c = new ContactListModel();	
		contactsList = c.getContactsList();	
	
	
	}

	/**
	 * Create a model to select only the contacts that are in the group/list of groups
	 * corresponding to the text entered by the user in the search field
	 * @param w
	 * @param text, to get the text searched for by the user
	 */
	public ByGroupController(MainWindow w, String text){
		
		this.w = w;
		this.text=text;		

		// Create a list with all the contacts of the file 
		ContactListModel c = new ContactListModel();	
		contactsList = c.getContactsList();
		
		this.setListByGroup();
	
	}

	public ArrayList<Contact> getContactsList() {
		return contactsList;
	}

	public void setContactsList(ArrayList<Contact> contactsList) {
		this.contactsList = contactsList;
	}

	/**
	 * Function called when the user do a search by group 
	 * Call the functions to actualize the display of the contact list.
	 */
	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		
		this.text = a.getSearch();

		m = new ByGroupModel(this);
		
		/* Set the variables StaticSearch and StaticWord with the value group and the value of 
		 * the variable text to display the right list according to the choice of the user
		 * The variable StaticWord save the string the user typed for the search.
		 * The variable StaticSearch save the type of search (by name, group, etc...).
		 */
		w.setStaticSearch("group");
		w.setStaticWord(text);

		w.setLayoutNull();
		
		if (contactsList.size()==0)
		{
			w.setLayoutNull();
		}
		else
		{
			w.setLayoutByGroup(this.contactsList);
		}
		
		
	}
	
	/**
	 * Call the functions to actualize the display of the contact list
	 */
	public void setListByGroup() {
		// TODO Auto-generated method stub

		m = new ByGroupModel(this);

		if (contactsList.size()==0)
		{
			w.setLayoutNull();
		}
		else
		{
			w.setLayoutByGroup(this.contactsList);
		}
		
		
		
	}


	public String getText() {
		return text;
	}


	public void setText(String text) {
		this.text = text;
	}

}
